﻿using AnesSystem.Models;
using AnesSystem.Services;
using AnesSystem.Udp;
using AnesSystem.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AnesSystem.Controls.ShuQian.PaiTai.CDTY
{
    /// <summary>
    /// ShouShuPaiTaiCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class SecheduleMainCtrl : UserControl
    {
        #region Variables

        #endregion

        #region Constructor

        public SecheduleMainCtrl()
        {
            InitializeComponent();
            this.DataContext = SecheduleMainCtrlViewModel.Instance;
            this.Loaded += SecheduleMainCtrl_Loaded;
        }

        #endregion

        #region Properties

        #endregion

        #region Event

        #endregion

        #region Private Methods
        private void getApply_Click(object sender, RoutedEventArgs e)
        {
            SecheduleMainCtrlViewModel.Instance.GetPatientLinksFromHIS();
        }

        #endregion

        #region Public/Protected Methods

        public void SetDataContext(object data)
        {
            this.DataContext = data;
        }

        #endregion

        #region EventHandlers

        private void SecheduleMainCtrl_Loaded(object sender, RoutedEventArgs e)
        {
            this.Ysxm.Text = "";
            SecheduleMainCtrlViewModel.Instance.Refresh();
        }


        #endregion

        private void refresh_Click(object sender, RoutedEventArgs e)
        {
            SecheduleMainCtrlViewModel.Instance.Refresh();
        }

        private void ceshi_Click(object sender, RoutedEventArgs e)
        {
            List<PatintLinkCopyModel> PatintLinkCopyList = PatientLinkCopyService.Instance.GetOutRoom();
            List<PatientLinkModel> PatintLinkAddList = new List<PatientLinkModel>();
            Dictionary<string, PatintLinkCopyModel> PatintLinkCopyMap = new Dictionary<string, PatintLinkCopyModel>();
            foreach (var PatintLinkCopy in PatintLinkCopyList)
            {
                string zyh = PatintLinkCopy.PatientLink.Patient.Brxm;
                if (!string.IsNullOrEmpty(zyh))
                {
                    if (PatintLinkCopyMap.ContainsKey(zyh))
                    {
                        if (DateTime.Compare(PatintLinkCopyMap[zyh].LastModifyTime, PatintLinkCopy.LastModifyTime) == -1)
                        {
                            PatintLinkCopyMap[zyh] = PatintLinkCopy;
                        }
                    }
                    else
                    {
                        PatintLinkCopyMap.Add(zyh, PatintLinkCopy);
                    }
                }
            }
            if (PatintLinkCopyMap.Count != 0)
            {
                foreach (var patintLinkCopy in PatintLinkCopyMap.Values)
                {
                    PatientLinkModel patientLink = patintLinkCopy.PatientLink;
                    patientLink.Id = new MongoDB.Bson.ObjectId();
                    PatientLinkService.Instance.Insert(patientLink);
                    //patientLink = PatientLinkService.Instance.GetPatientByZyh(patientLink.Patient.Zyh)[0];
                    //patientLink.Id = patintLinkCopy.PatientLink.Id;
                    //PatientLinkService.Instance.AddUpdate(patientLink);
                }
            }
        }
    }
}
